/**
 * Created by Administrator on 2017/12/4.
 */
import React from 'react';
import $ from 'jquery'
import {R_header_fixed} from './common/index'
import {Toast, HttpService} from '../Http'
import {
    pullDownRefresh,
} from '../Tool'
import {GetDate} from '../utils'
import LoadingBox from './common/LoadingBox'
export  default  class OrderManage extends React.Component {
    constructor(props) {
        super(props);
        this.page = 1;
        this.pagesize = 10;
        this.toBottom = 60;
        this.state = {
            dataList: [],
            dataLoad: 1,
            dataNull: 0,
            dataEnd: 0
        };
    }

    componentwillunmount(){
        window.onscroll =null;
    }
    componentDidMount() {
        this.getData()
        //加载更多
        // window.onscroll = () => {
        //     const {
        //         dataEnd,
        //         dataNull,
        //         page
        //     } = this.state;
        //     if (this.dataLoad == 1 || dataEnd == 1 || dataNull == 1)
        //         return;
        //     var scrollTop = 0;
        //     if (document.documentElement && document.documentElement.scrollTop) {
        //         scrollTop = document.documentElement.scrollTop;
        //     } else if (document.body) {
        //         scrollTop = document.body.scrollTop;
        //     }
        //     if (document.body.clientHeight - window.innerHeight - scrollTop <= this.toBottom) {
        //         this.page++;
        //         this.getData();
        //     }
        // }
        // const obj = document.getElementById('refreshbox')
        // const contentObj = document.getElementById('pdownrefresh')
        // const statusObj = document.getElementById('status')
        // let rct = this;
        // pullDownRefresh(obj, contentObj, statusObj, 70, function(data) {
        //     if (data.status == 'end' && data.top >= 50) {
        //         this.page = 1;
        //         this.getData(1);
        //     }
        // }.bind(this))
    }

    //获取数据列表
    getData(status) {
        this.setState({
            dataLoad: 1,
            dataEnd: 0,
            dataNull: 0
        });
        if (status == 1)
            this.page = 1
        HttpService.query({
            url: '/app/user/getOrdersNew',
            async:false,
            data: {
                token: localStorage.appToken,
                pageindex: this.page,
                pagesize: this.pagesize,

            },
            success: (res => {
                this.setState({
                    dataLoad: 0
                });
                if (res.code == "0") {
                    //获取数据成功
                    let dataList = res.data;
                    let orders=[];
                    for(let i=0;i<dataList.length;i++){
                        if(dataList[i].service=="purchase"){
                            dataList[i].createtime=GetDate.time(dataList[i].createtime)
                            orders.push(dataList[i])
                        }
                    }
                    if (status != 1)
                        dataList = this.state.dataList.concat(orders);
                    this.setState({
                        dataList
                    });
                    if (res.data.totalPages == this.page) {
                        this.setState({
                            dataEnd: 1
                        });
                    } else if (orders.length < this.pagesize) {
                        this.setState({
                            dataEnd: 1
                        });
                        if (dataList.length == 0) {
                            this.setState({
                                dataEnd: 0,
                                dataNull: 1
                            });
                        }
                    }
                    return;
                }
                // Toast(res.msg);
            })
        })
    }



    render() {
        const {dataList,  dataLoad, dataNull, dataEnd} = this.state;
        let flag=true
        let dataLists=dataList.length>0? dataList.map(function (item) {
                return (
                    <li className="list" key={item.id} style={{borderBottom:'solid 1px #ddd',padding:'20px 0'}}>
                        <div className="media media-item">
                            <div className="media-body">
                                {/*<div className="orderNumber">*/}
                                    {/*<span style={{fontSize:'28px'}}>{item.babyname}</span>*/}
                                    {/*<span style={{float:'right'}}>{item.createtime}</span>*/}
                                {/*</div>*/}
                                <div className="name" style={{fontSize:'24px',color:'#6d6d6d',margin:'20px 0'}}>
                                    <span >订单号：</span>
                                    <span>{item.orderno}</span>
                                </div>
                                <div style={{fontSize:'24px',color:'#6d6d6d',paddingBottom:'20px '}}>
                                    <span>购买金额：</span>
                                    <span>{"￥"+item.tradeamount.toFixed(2)}</span>
                                </div>
                                <div style={{fontSize:'24px',color:'#6d6d6d',paddingBottom:'20px '}}>
                                    <span>购买时间：</span>
                                    <span>{item.createtime}</span>
                                </div>
                            </div>
                        </div>

                    </li>
                )
        }.bind(this)):<li style={{marginTop:'300px',textAlign:'center'}}>当前无购买订单</li>

        return (
            <div className="order">
                <R_header_fixed left="1" title="购买订单"/>
                <div id='refreshbox'>
                    <div id='status'><span></span></div>
                    <div id='pdownrefresh'>
                        <div className="orderManage" style={{marginTop:'5rem'}}>
                            <ul style={{padding:'0 40px'}}>
                                {dataLists}
                            </ul>
                        </div>
                    </div>
                </div>

            </div>
        )
    }
}